//
//  MainTabBar.swift
//  SinaWeibo
//
//  Created by rk on 15/10/6.
//  Copyright © 2015年 kyy. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    //改变tabBar item的位置大小
    override func layoutSubviews() {
        super.layoutSubviews()
//        print(subviews)
    
        //定义item的frame
        let w = UIScreen.mainScreen().bounds.width / 5
        let h = self.bounds.height
        let frame = CGRectMake(0, 0, w, h)
        
        var index: CGFloat = 0
        for view in subviews {
            if view is UIControl && !(view is UIButton){
                
//                print(view)
                
                /**
                *  设置每一个item的具体frame
                *
                *  @param frame              视图的大小(view is UIControl)&& !(view is UIButton)
                *  @param CGFloat            X轴方向的位移
                *  @param CGFloat            Y轴方向的位移
                *
                *  @return frame
                */
            view.frame = CGRectOffset(frame, index * w, 0)
                if index == 1{
                index++
//                composeButton.frame = CGRectOffset(frame, index * w, 0)
                }
                index++
            }
        }
        
        composeButton.frame = CGRectOffset(frame, 2 * w, 0)
    }
    
    //通过懒加载 加载中间的➕按钮
    lazy var composeButton: UIButton = {  /*  () -> UIButton in  */
        
        //button初始化
    let btn = UIButton(type: UIButtonType.Custom)
        
    btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
    btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
    btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
    btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
    
    self.addSubview(btn)
    return btn
    }() // ()表示 要执行前面的闭包
    

}
